package com.kzj.controller;

import com.kzj.common.FailMsgConstant;
import com.kzj.common.GlobalConstant;
import com.kzj.myJar.common.result.Result;
import com.kzj.myJar.common.utils.StringUtil;
import com.kzj.myJar.common.utils.ValidateCodeUtil;
import com.kzj.myJar.springboot.redis.RedisJsonUtil;
import com.kzj.service.CommonService;
import com.kzj.service.FileTransferService;
import io.jsonwebtoken.lang.Assert;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.Cookie;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    CommonService commonService;
    @Autowired
    FileTransferService fileTransferService;
    @Value("${app.verify-code.period-of-validity}")
    int verifyCodePeriodOfValidity;
    @Deprecated
    @PostMapping("/generateGraphVerificationCode")
    public void generateGraphVerificationCode(HttpServletResponse response){
        try {
            String code = ValidateCodeUtil.generateVerifyImage(response.getOutputStream());
            //cookies生成并写入
            String uuid = UUID.randomUUID().toString();
            String key=GlobalConstant.COOKIES_KEY_PREFIX+uuid;
            RedisJsonUtil.set(key,code,verifyCodePeriodOfValidity, TimeUnit.MINUTES);
            response.addCookie(new Cookie(GlobalConstant.COOKIES_NAME,uuid));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/generateVerificationCodeImage")
    public Result generateVerificationCodeImage(HttpServletResponse response){
        try {
            String code = ValidateCodeUtil.generateVerifyNumberCode(4);
            byte[] bytes = ValidateCodeUtil.generateVerifyImage(code);
            String uuid = UUID.randomUUID().toString();
            String key=GlobalConstant.COOKIES_KEY_PREFIX+uuid;
            RedisJsonUtil.set(key,code,verifyCodePeriodOfValidity,TimeUnit.MINUTES);
            response.addCookie(new Cookie(GlobalConstant.COOKIES_NAME,uuid));
            return Result.success(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/sendEmailAuthCode")
    public Result sendEmailAuthCode(String email, String verifyCode, @CookieValue(GlobalConstant.COOKIES_NAME) String cookies){
        Assert.isTrue(StringUtil.isNotBlank(cookies), FailMsgConstant.ILLEGAL_REQUEST);
        String code = RedisJsonUtil.get(GlobalConstant.COOKIES_KEY_PREFIX + cookies);
        Assert.isTrue(StringUtil.isNotBlank(code),FailMsgConstant.VERIFY_CODE_NOT_FOUND);
        Assert.isTrue(code.equals(verifyCode),FailMsgConstant.VERIFY_CODE_ERROR);
        commonService.sendEmailAuthCode(email);
        return Result.success();
    }
    @PostMapping("/sendPhoneAuthCode")
    public Result sendPhoneAuthCode(String phone, String verifyCode, @CookieValue(GlobalConstant.COOKIES_NAME) String cookies){
        Assert.isTrue(StringUtil.isNotBlank(cookies), FailMsgConstant.ILLEGAL_REQUEST);
        String code = RedisJsonUtil.get(GlobalConstant.COOKIES_KEY_PREFIX + cookies);
        Assert.isTrue(StringUtil.isNotBlank(code),FailMsgConstant.VERIFY_CODE_NOT_FOUND);
        Assert.isTrue(code.equals(verifyCode),FailMsgConstant.VERIFY_CODE_ERROR);
        commonService.sendPhoneAuthCode(phone);
        return Result.success();
    }
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file){
        try {
            return Result.success(fileTransferService.upload(file.getInputStream(),file.getOriginalFilename()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
